<template>
  <div class="bgc_0003">
    <div class="content">
      <div class="content_bg">
        <img src="@/assets/imgs/tiaozhuan.png">
      </div>
      <div class="content_img">
        <img src="@/assets/imgs/weixin.png">
      </div>
      <div class="content_msg">
        <span class="content_text">等待微信授权中~</span>
        <img src="@/assets/imgs/jiazai.png" class="mt8 ml5">
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'LoggingPage',
    created () {
      let code = this.$route.query.code
      let userId = localStorage.getItem('userId')
      let card_stats = localStorage.getItem('card_stats')

      if (!code && !userId) {
        this.$wx.login()
      } else if (code && !userId) {
        this.register(code)
      } else if (userId) {
        this.check(userId)
      }
    },
    methods: {
      register (code) {
        this.$wx.register(code).then(v => {
          let openid = v.openid
          let userId = v.user_id
          let card_stats = v.card_stats
          if (openid) {
            localStorage.setItem('openid', openid)
          }
          if (userId) {
            localStorage.setItem('userId', userId)
          }
          if (card_stats) {
            localStorage.setItem('card_stats', card_stats)
          }

          this.check(userId)
        })
      },
      check (userId) {
        $Util.loading()
        this.$inbound.invoke({
          interfaceId: 'app.member.queryCardStats',
          data: {user_id: userId}
        }).then(v => {
          $Util.hideloading()
          localStorage.setItem("card_stats", v.card_stats)
          if (v.card_stats == 0) {
            this.$router.replace({name: 'BindingPage'})
          } else {
            this.$router.replace({path: '/'})
          }
        }).catch(e => {
          $Util.hideloading()
        })
      }
    }
  }
</script>

<style scoped>

  .content {
    float: left;
    margin: 180px 0 0 30px;
  }

  .content_bg {
    float: left;
    position: absolute;
    z-index: -100;
  }

  .content_img {
    float: left;
    width: 100%;
    position: relative;
    z-index: -99;
    left: 112px;
    top: 40px;
  }

  .content_msg {
    float: left;
    position: relative;
    z-index: -99;
    left: 58px;
    top: 105px;
  }

  .content_text {
    float: left;
    font-family: 'pingfang bold';
    font-size: 15px;
    color: #ff0000;
    line-height: 34px;
    margin-left: 15px;
  }

</style>
